/*******************************************************************************
 * @brief: Modbus app for RTU
 * @Author:   aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-06 22:09:52
 * @Verision: 1.0.0.0
 ******************************************************************************/
/* ----------------------- Platform includes --------------------------------*/
#define _MAPP_MODULE_
#include "mbapp.h"
#include "os_obj.h"
#include "sys_init.h"
#include "sys_task.h"
#include "bsp.h"
#include "framework.h"

void qc_link(void);
void qc_init(void);
static uu8 inited = 0;
/******************************************************************************
 * @brief   mb handle.
 * @param   site, err
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void qc_callback(uu8 site, uu8 err)
{
	if (err)
	{
	}
}

qc_cmd_type mcmd;

/*******************************************************************************
 * @brief: qc task
 * @param {*}
 * @return {*}
 ******************************************************************************/
void modbus_task(void const *argument)
{
	osDelay(ms_ticks(50));
	qc_init();
	
	while(1)
	{
		qc_link();
		qc_mb_poll(&mb.qc02);
		qc_mb_poll(&mb.qc03);
		
		osSignalWait(0,ms_ticks(100));
	}
}

/*******************************************************************************
 * @brief: qc link
 * @param {*}
 * @return {*}
 ******************************************************************************/
void qc_link(void)
{
	mb_reg_dev_type = 0x1000;
	mb_reg_dev_fun = 0x1;
	mb_reg_hal_version = 0x0100;
	mb_reg_soft_version = 0x0100;
}

/******************************************************************************
 * @brief   qc_init.
 * @param   none
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void qc_init(void)
{
	// master config
	qc03_Init(QC_MODE_MASTER, 115200, MB_PAR_NONE);
	mb.qc03.os_event_send = mb3_os_send;

	mcmd.id = 1;
	mcmd.wdat = &mb_input[0].uval;
	mcmd.rdat = &mb_input[0].uval;
	mcmd.wa = 0;
	mcmd.wn = 96;
	mcmd.ra = 0x1800;
	mcmd.rn = 1;
	mcmd.callback = qc_callback;
	mcmd.attr = QC_MB_ATTR_HOLD_MW;
	mqc_stc_cmd_req(&mb.qc03, 0, &mcmd);
	
	mcmd.wdat = &mb_input[0].uval;
	mcmd.wa = 96;
	mcmd.wn = 96;
	mqc_stc_cmd_req(&mb.qc03, 1, &mcmd);

	// slave config
	qc02_Init(QC_MODE_SLAVE, 115200, MB_PAR_NONE);
	mb.qc02.os_event_send = mb2_os_send;

	mcmd.id = 1;
	mcmd.wdat = &mb_input[0].uval;
	mcmd.rdat = &mb_input[0].uval;
	mcmd.wa = 16;
	mcmd.wn = 16;
	mcmd.ra = 0;
	mcmd.rn = 8;
	mcmd.callback = qc_callback;
	mcmd.attr = QC_MB_ATTR_HOLD_MW;
	mqc_stc_cmd_req(&mb.qc02, 0, &mcmd);
	mqc_stc_cmd_req(&mb.qc02, 1, &mcmd);
}
